<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>输入id号返回数据对象</title>
</head>

<body>

</body>
<script>
  "use strict"; {
    let data = [{
      id: 1,
      name: "家电",
      goods: [{
        id: 11,
        name: "冰箱",
        goods: [{
          id: 111,
          name: "海尔"
        }, {
          id: 112,
          name: "美的"
        }]
      }]
    }, {
      id: 2,
      name: "电子",
      goods: [{
        id: 21,
        name: "手机",
        goods: [{
          id: 211,
          name: "苹果"
        }]
      }, {
        id: 22,
        name: "pc"
      }]
    }];
    // 我们输入id号，就可以返回的数据对象
    
    const get_id = (goods, id) => {
      let obj = null;
      goods.forEach((value, index, arr) => {
        if (value.id == id) {
          obj = value;
        } else if (value.goods && value.goods.length != 0) {
          obj = get_id(value.goods, id);
        };
      });
      return obj;
    };

    let i = get_id(data, 11);
    console.log(i);
    console.log(JSON.stringify(i, null, 2));
  };  
</script>

</html>